<?php if(! defined('HAS_POWER') ) exit('无权访问!');
// 这是一个自动生成的测试类和方法，请自行修改或者删除
class IndexController extends Controller {

    public function index()
    {
        $this->loadExtend('unit/foo/test', '.php');
        $a = new A();
        $a->abc();
        $this->render('index');
    }

    public function insert()
    {
        $user = $this->loadModel('user');

        $config = array(
            array(
                'field' => 'name',
                'label' => '姓名',
                'rules' => 'required',
            ),
            array(
                'field' => 'age',
                'label' => '年龄',
                'rules' => 'required',
            ),
            array(
                'field' => 'job',
                'label' => '工作',
                'rules' => 'required',
            ),
        );

        if($this->form->isPost())
        {
            if($this->form->validate($config))
            {
                $data = $this->form->create($user);
                $result = $user->insertUser($data);

                if($result)
                {
                    // TODO: 成功逻辑
                }
                else
                {
                    // TODO: 失败逻辑
                }
            }
        }
        $this->render('insert');
    }

    public function update()
    {
        $user = $this->loadModel('user');
        $id = $this->input->get('id');

        $data = $user->findUser($id);

        $config = array(
            array(
                'field' => 'name',
                'label' => '姓名',
                'rules' => 'required',
            ),
            array(
                'field' => 'age',
                'label' => '年龄',
                'rules' => 'required',
            ),
            array(
                'field' => 'job',
                'label' => '工作',
                'rules' => 'required',
            ),
        );

        if($this->form->isPost())
        {
            if($this->form->validate($config))
            {
                $data = $this->form->create($user);
                $result = $user->updateUser($data);

                if($result)
                {
                    // TODO: 成功逻辑
                }
                else
                {
                    // TODO: 失败逻辑
                }
            }
        }
        else
        {
            $this->render('update', array('data' => $data));
        }
    }

    public function delete()
    {
        $user = $this->loadModel('user');
        $id = $this->input->get('id');

        $result = $user->deleteUser($id);

        if($result)
        {
            // TODO: 成功逻辑
        }
        else
        {
            // TODO: 失败逻辑
        }
    }

    public function testrbac()
    {
        $this->loadComponent('rbac');
        $this->rbac->init();
    }
}
